package com.yanqu.road.server.gameplayer.module.activity.goodsLimit;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.goodsLimit.GoodsLimitData;
import com.yanqu.road.logic.bussiness.activity.GoodsLimitBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.util.HashMap;
import java.util.Map;

public class GoodsLimitModule extends GeneralModule {

    /**
     * 道具使用数据
     */
    private Map<Integer, GoodsLimitData> goodsLimitDataMap = new HashMap<>();

    public GoodsLimitModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        goodsLimitDataMap = GoodsLimitBussiness.getGoodsLimitDataMap(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        for (GoodsLimitData goodsLimitData : goodsLimitDataMap.values()) {
            if (goodsLimitData.isInsertOption()) {
                GoodsLimitBussiness.addGoodsLimitData(goodsLimitData);
            } else if (goodsLimitData.isUpdateOption()) {
                GoodsLimitBussiness.updateGoodsLimitData(goodsLimitData);
            }
        }
        return true;
    }

    public GoodsLimitData getGoodsLimitData(int goodsId) {
        return goodsLimitDataMap.get(goodsId);
    }

    /**
     * 获取道具的使用数量
     */
    public long getGoodsUseCount(int goodsId, int activityId) {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo == null) {
            return 0;
        }
        GoodsLimitData goodsLimitData = getGoodsLimitData(goodsId);
        if (goodsLimitData != null) {
            if (goodsLimitData.getActivityId() != activityId) {
                // 活动换了，需要重置下
                goodsLimitData.setActivityId(activityId);
                goodsLimitData.setCount(0);
            }
            return goodsLimitData.getCount();
        }
        return 0;
    }

    /**
     * 更改使用数量
     * @param goodsId
     * @param activityId
     * @param count 负 扣 正 加
     */
    public void updateGoodsUseCount(int goodsId, int activityId, long count) {
        GoodsLimitData goodsLimitData = getGoodsLimitData(goodsId);
        if (goodsLimitData == null) {
            goodsLimitData = new GoodsLimitData();
            goodsLimitData.setUserId(player.getUserId());
            goodsLimitData.setGoodsId(goodsId);
            goodsLimitData.setCount(0);
            goodsLimitData.setActivityId(activityId);
            goodsLimitData.setUpdateTime(System.currentTimeMillis());
            goodsLimitData.setInsertOption();
        }
        goodsLimitData.setCount(Math.max(0, goodsLimitData.getCount() + count));
        goodsLimitData.setUpdateTime(System.currentTimeMillis());
        goodsLimitDataMap.put(goodsId, goodsLimitData);
    }

}
